﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;

using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using YouHP.Utility;
using System.Collections.Generic;

namespace web.admin
{
    public partial class AdministratorAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void CancelButton_click(object sender, EventArgs e)
        {
            Response.Redirect("AdministratorList.aspx");
        }
        protected void InsertButton_click(object sender, EventArgs e)
        {
            List<UserInfo> info = validateForm(this.administatorInfo);

            if (info != null)
            {
                foreach (UserInfo userinfo in info)
                {
                    UserBLL.Instance.UpdateNormalUserToAdministrator(userinfo.Id);
                }
            }
            else
            {
                Response.Write("<script>alert('User is not exist');</script>");
            }
            Response.Redirect("AdministratorAdd.aspx");

        }
        protected void DataBound(object sender, EventArgs e)
        {

        }
        private List<UserInfo> validateForm(DetailsView formViewCtl)
        {
            List<UserInfo> info = new List<UserInfo>();
            int userid = 0;

            bool userIsExist = false;
            //info.IsNews = Convert.ToInt32(this.GroupId.Text);
            string tmpStr = ((TextBox)formViewCtl.FindControl("用户名")).Text.Trim();
            string[] tmpStr1 = tmpStr.Split(';');
            for (int i = 0; i < tmpStr1.Length; i++)
            {
                if (UserBLL.Instance.CheckExsitUserName(tmpStr1[i]))
                {
                    UserInfo administrator = new UserInfo();
                    userid = UserBLL.Instance.GetUserIdByUserName(tmpStr1[i]);
                    administrator.Id = userid;
                    administrator.UserName = tmpStr1[i];
                    info.Add(administrator);
                }
            }

            return info;
        }
    }
}
